<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任编辑" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入责任编辑"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任部门" prop="videoOrigin">
        <el-input
          v-model="queryParams.videoOrigin"
          placeholder="请输入责任部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="视频类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择视频类型" clearable>
          <el-option
            v-for="dict in dict.type.video_kind"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['videoRecoverStation:videoRecoverStation:remove']"
        >彻底删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          :disabled="multiple"
          @click="handleRecover"
          v-hasPermi="['videoRecoverStation:videoRecoverStation:recover']"
        >恢复数据</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['videoRecoverStation:videoRecoverStation:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videoRecoverStationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="视频类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.video_kind" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="file">
        <template slot-scope="scope">
          <video :src="scope.row.file" :width="150" :height="100" @click="playVideo(scope.row.file)" style="cursor: pointer;"/>
        </template>
      </el-table-column>
      <el-table-column label="封面" align="center" prop="coverImg" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverImg" :width="150" :height="100" v-if="scope.row.coverImg"/>
          <span v-else>无封面</span>
        </template>
      </el-table-column>
      <el-table-column label="责任编辑" align="center" prop="author" />
      <el-table-column label="责任部门" align="center" prop="videoOrigin" />
      <el-table-column label="日期" align="center" prop="pubdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pubdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- 有效日期 -->
      <el-table-column label="有效期" align="center" prop="validDate">
        <template slot-scope="scope">
          <span style="color: red;">{{ scope.row.validDate}}天</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="排序" align="center" prop="sortOrder" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleRecover(scope.row)"
          >恢复数据</el-button>
<!--            v-hasPermi="['videoRecoverStation:videoRecoverStation:recover']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['videoRecoverStation:videoRecoverStation:remove']"
          >彻底删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listVideoRecoverStation, getVideoRecoverStation, delVideoRecoverStation, addVideoRecoverStation, updateVideoRecoverStation, recoverVideoData } from "@/api/videoRecoverStation/videoRecoverStation";

export default {
  name: "VideoRecoverStation",
  dicts: ['video_kind'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频回收站表格数据
      videoRecoverStationList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        author: null,
        videoOrigin: null,
        type: null,
      }
    };
  },
  created() {
    this.getList();
    // 监听数据删除事件
    this.$bus.$on('videoDataDeleted', () => {
      console.log('回收站页面收到删除事件，开始刷新列表');
      this.getList();
    });
  },
  beforeDestroy() {
    // 移除事件监听
    this.$bus.$off('videoDataDeleted');
  },
  methods: {
    /** 查询视频回收站列表 */
    getList() {
      this.loading = true;
      listVideoRecoverStation(this.queryParams).then(response => {
        this.videoRecoverStationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除视频回收站编号为"' + ids + '"的数据项？').then(function() {
        return delVideoRecoverStation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 批量恢复按钮操作 */
    handleRecover(row) {
      const ids = row.id || this.ids;
      console.log(ids);
      this.$modal.confirm('是否确认恢复视频回收站编号为"' + ids + '"的数据项？').then(function() {
        return recoverVideoData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("恢复成功");
        // 触发事件通知视频页面刷新
        console.log('触发恢复事件: videoDataRecovered');
        this.$bus.$emit('videoDataRecovered');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('videoRecoverStation/videoRecoverStation/export', {
        ...this.queryParams
      }, `videoRecoverStation_${new Date().getTime()}.xlsx`)
    },
    /** 播放视频 */
    playVideo(videoUrl) {
      // 创建一个模态框来播放视频
      const videoHtml = `<video id="videoPlayer" src="${videoUrl}" controls style="max-width: 100%; max-height: 80vh; width: 100%;"></video>`;

      this.$alert(videoHtml, '视频播放', {
        dangerouslyUseHTMLString: true,
        showCancelButton: false,
        showConfirmButton: true,
        confirmButtonText: '关闭',
        closeOnClickModal: true,
        customClass: 'video-modal',
        callback: action => {
          // 关闭模态框时停止视频播放
          const videoPlayer = document.getElementById('videoPlayer');
          if (videoPlayer) {
            videoPlayer.pause();
            videoPlayer.currentTime = 0;
          }
        },
        beforeClose: (action, instance, done) => {
          // 无论哪种方式关闭模态框都停止视频播放
          const videoPlayer = document.getElementById('videoPlayer');
          if (videoPlayer) {
            videoPlayer.pause();
            videoPlayer.currentTime = 0;
          }
          done();
        }
      });
    }
  }
};
</script>
